package cn.hamster3.hamsterworld.command;

import cn.hamster3.hamsterworld.core.DataManager;
import cn.hamster3.hamsterworld.core.Message;
import cn.hamster3.hamsterworld.data.WorldData;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

@SuppressWarnings("deprecation")
public class AddCommand extends CommandBase {
    public AddCommand(DataManager dataManager) {
        super(dataManager);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        Player player = (Player) sender;
        WorldData worldData = dataManager.getWorldData(player);
        if (worldData == null) {
            player.sendMessage(Message.notHasWorld.toString());
            return true;
        }
        if (args.length < 2) {
            player.sendMessage(Message.notInputPlayerName.toString());
            return true;
        }
        OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(args[1]);
        if (offlinePlayer == null) {
            player.sendMessage(Message.playerNotExist.toString());
            return true;
        }
        worldData.allowPlayer(offlinePlayer.getUniqueId());
        player.sendMessage(Message.addSuccess.toString().replace("%player%", offlinePlayer.getName()));
        return true;
    }
}
